package com.mia.props.client.container;

import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.TabProps;
import com.mia.props.client.container.guilib.GuiDisableableButton;
import com.mia.props.client.container.guilib.GuiElement;
import com.mia.props.client.container.guilib.GuiGauge;
import com.mia.props.client.container.guilib.GuiLabel;
import com.mia.props.client.container.guilib.GuiTextBox;
import com.mia.props.client.container.guilib.GuiToggleButton;
import com.mia.props.client.container.guilib.IExposedGui;
import com.mia.props.client.container.guilib.IGuiHandlesMouseClick;
import com.mia.props.client.container.guilib.IGuiRenderable;
import com.mia.props.client.container.guilib.IGuiTooltip;
import com.mia.props.client.container.guilib.TextAlign;
import com.mia.props.common.entities.TileDecobench;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
public class GuiDecobench extends GuiContainer implements IExposedGui {
   static final ResourceLocation texture = new ResourceLocation("props", "textures/gui/table.png");
   static final int i_w = 8;
   static final int i_h = 5;
   final EntityPlayer player;
   final TileDecobench table;
   float prevScroll = -1.0F;
   float currentScroll = 0.0F;
   boolean isScrolling = false;
   TabProps currentTab;
   final GuiElement scrollBar;
   final GuiElement scrollArea;
   final GuiElement slotInput;
   final GuiElement slotOutput;
   final GuiToggleButton checkboxTabFilter;
   final GuiDisableableButton buttonArrowLeft;
   final GuiDisableableButton buttonArrowRight;
   final GuiDisableableButton buttonAddResources;
   final GuiGauge[] gaugeResources;
   final GuiTextBox searchBox;
   final GuiLabel labelTitle;
   final GuiLabel labelTabFilter;
   final GuiLabel labelCurrentTab;
   ArrayList allItems;
   final ArrayList currentItems;
   final ArrayList decoButtons;
   final ArrayList guiClickables;
   final ArrayList guiRenderables;
   final ArrayList textRenderables;
   final ArrayList guiTooltips;
   final ArrayList guiUpdatables;

   public GuiDecobench(ContainerDecobench containerTable) {
      super(containerTable);
      this.currentTab = TabProps.Art;
      this.currentItems = new ArrayList();
      this.decoButtons = new ArrayList(40);
      this.guiClickables = new ArrayList();
      this.guiRenderables = new ArrayList();
      this.textRenderables = new ArrayList();
      this.guiTooltips = new ArrayList();
      this.guiUpdatables = new ArrayList();
      this.player = containerTable.player;
      this.table = containerTable.table;
      this.xSize = 202;
      this.ySize = 223;
      this.scrollBar = new GuiElement(this, 181, 48, 13, 88);
      this.scrollArea = new GuiElement(this, 33, 47, 162, 90);
      this.slotInput = new GuiElement(this, 9, 9, 18, 18);
      this.slotOutput = new GuiElement(this, 9, 119, 18, 18);
      this.checkboxTabFilter = new GuiToggleButton(this, 33, 30, 15, 15, new int[]{225, 197, 241, 197, 225, 181, 241, 181}, false) {
         protected void mouseClickedCallback(int mouseButton) {
            GuiDecobench.this.buttonArrowLeft.setDisabled(!this.toggledOn);
            GuiDecobench.this.buttonArrowRight.setDisabled(!this.toggledOn);
            GuiDecobench.this.labelTabFilter.setColor(this.toggledOn ? 4210752 : 10526880);
            GuiDecobench.this.labelCurrentTab.setColor(this.toggledOn ? 4210752 : 10526880);
            GuiDecobench.this.updateAllItems();
         }
      };
      this.buttonArrowLeft = new GuiDisableableButton(this, 111, 30, 10, 15, new int[]{192, 241, 203, 241, 214, 241}, true) {
         protected void mouseClickedCallback(int mouseButton) {
            int next = GuiDecobench.this.currentTab.ordinal() - 1;
            if (next < 1) {
               next = TabProps.values().length - 1;
            }

            GuiDecobench.this.currentTab = TabProps.values()[next];
            GuiDecobench.this.labelCurrentTab.setText(StatCollector.translateToLocal("text.props.gui.tab." + GuiDecobench.this.currentTab.name()));
            GuiDecobench.this.updateAllItems();
         }
      };
      this.buttonArrowRight = new GuiDisableableButton(this, 185, 30, 10, 15, new int[]{192, 225, 203, 225, 214, 225}, true) {
         protected void mouseClickedCallback(int mouseButton) {
            int next = GuiDecobench.this.currentTab.ordinal() + 1;
            if (next == TabProps.values().length) {
               next = 1;
            }

            GuiDecobench.this.currentTab = TabProps.values()[next];
            GuiDecobench.this.labelCurrentTab.setText(StatCollector.translateToLocal("text.props.gui.tab." + GuiDecobench.this.currentTab.name()));
            GuiDecobench.this.updateAllItems();
         }
      };
      this.buttonAddResources = new GuiDisableableButton(this, 12, 29, 12, 8, new int[]{228, 154, 228, 163, 228, 172}, true) {
         public void update() {
            if (GuiDecobench.this.inventorySlots.getSlot(0).getHasStack()) {
               this.tooltipEnabled = true;
               this.tooltipText.clear();
               this.tooltipText.add(StatCollector.translateToLocal("text.props.gui.resources.add"));
               boolean space = GuiDecobench.this.table.roomForResources(TileDecobench.resourcesProvidedByStack(GuiDecobench.this.inventorySlots.getSlot(0).getStack()));
               this.disabled = !GuiScreen.isShiftKeyDown() && !space;
               if (!space) {
                  if (this.disabled) {
                     this.tooltipText.add(EnumChatFormatting.DARK_GRAY.toString() + EnumChatFormatting.ITALIC + StatCollector.translateToLocal("text.props.gui.resources.add_loss_warning"));
                  } else {
                     this.tooltipText.add(EnumChatFormatting.AQUA.toString() + EnumChatFormatting.BOLD + StatCollector.translateToLocal("text.props.gui.resources.add_loss"));
                  }
               }
            } else {
               this.tooltipEnabled = false;
               this.disabled = true;
            }

         }

         protected void mouseClickedCallback(int mouseButton) {
            if (mouseButton == 0) {
               GuiDecobench.this.table.onData(GuiDecobench.this.mc.thePlayer, -1, GuiScreen.isShiftKeyDown());
            }

         }
      };
      this.gaugeResources = new GuiGauge[4];
      this.gaugeResources[0] = new GuiGauge(this, 7, 40, 22, 21, new int[]{0, 0, 169, 235}, false, false, false) {
         private int last;

         {
            this.tooltipEnabled = true;
            this.last = -1;
         }

         public void update() {
            if (this.last != GuiDecobench.this.table.getResource(0)) {
               this.last = GuiDecobench.this.table.getResource(0);
               this.setFill((float)GuiDecobench.this.table.getResource(0) / (float)GuiDecobench.this.table.MAX_STORE[0]);
               this.tooltipText.clear();
               this.tooltipText.add(EnumChatFormatting.GRAY + StatCollector.translateToLocal("text.props.gui.resources.clay"));
               this.tooltipText.add(EnumChatFormatting.WHITE + String.format("%d / %d", GuiDecobench.this.table.getResource(0), GuiDecobench.this.table.MAX_STORE[0]));
            }
         }
      };
      this.gaugeResources[1] = new GuiGauge(this, 7, 88, 8, 28, new int[]{123, 228, 146, 228}, false, false, true) {
         private int last;

         {
            this.tooltipEnabled = true;
            this.last = -1;
         }

         public void update() {
            if (this.last != GuiDecobench.this.table.getResource(1)) {
               this.last = GuiDecobench.this.table.getResource(1);
               this.setFill((float)GuiDecobench.this.table.getResource(1) / (float)GuiDecobench.this.table.MAX_STORE[1]);
               this.tooltipText.clear();
               this.tooltipText.add(EnumChatFormatting.RED + StatCollector.translateToLocal("text.props.gui.resources.red"));
               this.tooltipText.add(EnumChatFormatting.WHITE + String.format("%d / %d", GuiDecobench.this.table.getResource(1), GuiDecobench.this.table.MAX_STORE[1]));
            }
         }
      };
      this.gaugeResources[2] = new GuiGauge(this, 14, 88, 8, 28, new int[]{130, 228, 153, 228}, false, false, true) {
         private int last;

         {
            this.tooltipEnabled = true;
            this.last = -1;
         }

         public void update() {
            if (this.last != GuiDecobench.this.table.getResource(2)) {
               this.last = GuiDecobench.this.table.getResource(2);
               this.setFill((float)GuiDecobench.this.table.getResource(2) / (float)GuiDecobench.this.table.MAX_STORE[2]);
               this.tooltipText.clear();
               this.tooltipText.add(EnumChatFormatting.GREEN + StatCollector.translateToLocal("text.props.gui.resources.green"));
               this.tooltipText.add(EnumChatFormatting.WHITE + String.format("%d / %d", GuiDecobench.this.table.getResource(2), GuiDecobench.this.table.MAX_STORE[2]));
            }
         }
      };
      this.gaugeResources[3] = new GuiGauge(this, 21, 88, 8, 28, new int[]{137, 228, 160, 228}, false, false, true) {
         private int last;

         {
            this.tooltipEnabled = true;
            this.last = -1;
         }

         public void update() {
            if (this.last != GuiDecobench.this.table.getResource(3)) {
               this.last = GuiDecobench.this.table.getResource(3);
               this.setFill((float)GuiDecobench.this.table.getResource(3) / (float)GuiDecobench.this.table.MAX_STORE[3]);
               this.tooltipText.clear();
               this.tooltipText.add(EnumChatFormatting.BLUE + StatCollector.translateToLocal("text.props.gui.resources.blue"));
               this.tooltipText.add(EnumChatFormatting.WHITE + String.format("%d / %d", GuiDecobench.this.table.getResource(3), GuiDecobench.this.table.MAX_STORE[3]));
            }
         }
      };
      this.searchBox = new GuiTextBox(this, 34, 17, 160, 10, StatCollector.translateToLocal("text.props.gui.search"), 4210752, 16777215);
      this.labelTitle = new GuiLabel(this, 101, 5, StatCollector.translateToLocal("text.props.gui.title"), TextAlign.CENTER, 2105376);
      this.labelTabFilter = new GuiLabel(this, 50, 34, StatCollector.translateToLocal("text.props.gui.filter.tab"), TextAlign.LEFT, 10526880);
      this.labelCurrentTab = new GuiLabel(this, 153, 34, StatCollector.translateToLocal("text.props.gui.tab." + this.currentTab.name()), TextAlign.CENTER, 10526880);
      this.guiClickables.add(this.checkboxTabFilter);
      this.guiClickables.add(this.buttonArrowLeft);
      this.guiClickables.add(this.buttonArrowRight);
      this.guiClickables.add(this.buttonAddResources);
      this.guiRenderables.add(this.checkboxTabFilter);
      this.guiRenderables.add(this.buttonArrowLeft);
      this.guiRenderables.add(this.buttonArrowRight);
      this.guiRenderables.add(this.buttonAddResources);
      this.textRenderables.add(this.searchBox);
      this.textRenderables.add(this.labelTitle);
      this.textRenderables.add(this.labelTabFilter);
      this.textRenderables.add(this.labelCurrentTab);
      this.guiTooltips.add(this.buttonAddResources);
      this.guiUpdatables.add(this.buttonAddResources);
      GuiGauge[] var2 = this.gaugeResources;
      int var3 = var2.length;

      for(int var4 = 0; var4 < var3; ++var4) {
         GuiGauge gauge = var2[var4];
         this.guiRenderables.add(gauge);
         this.guiTooltips.add(gauge);
         this.guiUpdatables.add(gauge);
      }

      this.updateAllItems();
   }

   private void updateAllItems() {
      this.allItems = new ArrayList();
      ArrayList metaList = new ArrayList(Props.modelData.values());
      Collections.sort(metaList);
      Iterator var2 = metaList.iterator();

      while(true) {
         DecoModelMetadata data;
         do {
            if (!var2.hasNext()) {
               this.currentScroll = 0.0F;
               this.refreshButtons();
               return;
            }

            data = (DecoModelMetadata)var2.next();
         } while(this.checkboxTabFilter.isToggledOn() && (!this.checkboxTabFilter.isToggledOn() || data.tab != this.currentTab));

         if (!data.hideDecobench) {
            this.allItems.add(new ItemStack(Props.itemProps, 1, data.decocraftModelID));
         }
      }
   }

   public void refreshButtons() {
      this.currentItems.clear();
      if (this.searchBox.text.trim().isEmpty()) {
         this.currentItems.addAll(this.allItems);
      } else {
         String searchName = this.searchBox.text.trim().toLowerCase();
         Iterator var2 = this.allItems.iterator();

         while(var2.hasNext()) {
            ItemStack stack = (ItemStack)var2.next();
            if (stack.getDisplayName().toLowerCase().contains(searchName)) {
               this.currentItems.add(stack);
            }
         }
      }

      this.guiClickables.removeAll(this.decoButtons);
      this.guiTooltips.removeAll(this.decoButtons);
      this.decoButtons.clear();
      int i = this.currentItems.size() / 8 - 5 + 1;
      int j = (int)((double)(this.currentScroll * (float)i) + 0.5D);

      for(int y = 0; y < 5; ++y) {
         for(int x = 0; x < 8; ++x) {
            int idx = this.currentItems.size() < 40 ? x + y * 8 : x + (y + j) * 8;
            if (idx >= 0 && idx < this.currentItems.size()) {
               this.decoButtons.add(new GuiDecobench.DecoButton((ItemStack)this.currentItems.get(idx), 34 + 18 * x, 48 + 18 * y));
            }
         }
      }

      this.guiClickables.addAll(this.decoButtons);
      this.guiTooltips.addAll(this.decoButtons);
   }

   public void drawGuiContainerBackgroundLayer(float pt, int mouseX, int mouseY) {
      this.mc.getTextureManager().bindTexture(texture);
      GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
      this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
      this.drawTexturedModalRect(this.scrollBar.posX(), this.scrollBar.posY() + (int)(this.currentScroll * (float)(this.scrollBar.getHeight() - 11)), 243, 169, 13, 11);
   }

   protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
      GL11.glPushMatrix();
      GL11.glTranslatef((float)(-this.guiLeft), (float)(-this.guiTop), 0.0F);
      GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
      Iterator var3 = this.guiUpdatables.iterator();

      while(var3.hasNext()) {
         GuiElement element = (GuiElement)var3.next();
         element.update();
      }

      boolean mouse = Mouse.isButtonDown(0);
      if (this.isScrolling) {
         if (mouse) {
            this.currentScroll = (float)(mouseY - this.scrollBar.posY() - 5) / (float)(this.scrollBar.getHeight() - 11);
            this.currentScroll = MathHelper.clamp_float(this.currentScroll, 0.0F, 1.0F);
            if (this.prevScroll != this.currentScroll) {
               this.prevScroll = this.currentScroll;
               this.refreshButtons();
            }
         } else {
            this.isScrolling = false;
         }
      } else if (mouse && this.scrollBar.mouseOver(mouseX, mouseY)) {
         this.isScrolling = true;
      }

      Iterator var8 = this.decoButtons.iterator();

      while(var8.hasNext()) {
         GuiDecobench.DecoButton d = (GuiDecobench.DecoButton)var8.next();
         d.render(mouseX, mouseY);
      }

      this.mc.getTextureManager().bindTexture(texture);
      GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
      var8 = this.guiRenderables.iterator();

      IGuiRenderable renderable;
      while(var8.hasNext()) {
         renderable = (IGuiRenderable)var8.next();
         renderable.render(mouseX, mouseY);
      }

      var8 = this.textRenderables.iterator();

      while(var8.hasNext()) {
         renderable = (IGuiRenderable)var8.next();
         renderable.render(mouseX, mouseY);
      }

      ArrayList list = new ArrayList();
      Iterator var11 = this.guiTooltips.iterator();

      while(var11.hasNext()) {
         IGuiTooltip tippedGui = (IGuiTooltip)var11.next();
         if (tippedGui.mouseOver(mouseX, mouseY) && tippedGui.isTooltipEnabled()) {
            list.addAll(tippedGui.getTooltipText(mouseX, mouseY));
         }
      }

      if (this.slotInput.mouseOver(mouseX, mouseY) && !this.inventorySlots.getSlot(0).getHasStack()) {
         list.add(StatCollector.translateToLocal("text.props.gui.slot.input"));
      } else if (this.slotOutput.mouseOver(mouseX, mouseY) && !this.inventorySlots.getSlot(1).getHasStack()) {
         list.add(StatCollector.translateToLocal("text.props.gui.slot.output"));
      }

      this.drawHoveringText(list, mouseX, mouseY, this.fontRendererObj);
      RenderHelper.enableGUIStandardItemLighting();
      GL11.glPopMatrix();
   }

   public void handleMouseInput() {
      super.handleMouseInput();
      int i = Mouse.getEventDWheel();
      if (i != 0) {
         this.refreshButtons();
         if (this.currentItems.size() > this.decoButtons.size()) {
            int mouseX = Mouse.getEventX() * this.width / this.mc.displayWidth;
            int mouseY = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1;
            if (this.scrollArea.mouseOver(mouseX, mouseY)) {
               int j = this.currentItems.size() / 8 - 5 + 1;
               if (i > 1) {
                  i = 1;
               } else if (i < -1) {
                  i = -1;
               }

               this.currentScroll = MathHelper.clamp_float(this.currentScroll - (float)i / (float)j, 0.0F, 1.0F);
               if (this.prevScroll != this.currentScroll) {
                  this.prevScroll = this.currentScroll;
                  this.refreshButtons();
               }
            }
         }

      }
   }

   public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
      if (this.searchBox.mousePressed(mouseX, mouseY, mouseButton)) {
         this.refreshButtons();
      } else {
         Iterator var4 = this.guiClickables.iterator();

         IGuiHandlesMouseClick clickHandler;
         do {
            if (!var4.hasNext()) {
               super.mouseClicked(mouseX, mouseY, mouseButton);
               return;
            }

            clickHandler = (IGuiHandlesMouseClick)var4.next();
         } while(!clickHandler.mouseOver(mouseX, mouseY));

         clickHandler.mouseClicked(mouseButton);
      }
   }

   public void keyTyped(char keyChar, int key) {
      if (this.searchBox.keyPressed(key, keyChar)) {
         this.refreshButtons();
      } else {
         super.keyTyped(keyChar, key);
      }
   }

   public boolean isMouseOverButtonScrollArea() {
      ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
      int i = scaledresolution.getScaledWidth();
      int j = scaledresolution.getScaledHeight();
      int k = Mouse.getX() * i / this.mc.displayWidth;
      int l = j - Mouse.getY() * j / this.mc.displayHeight - 1;
      return this.scrollArea.mouseOver(k, l);
   }

   public GuiContainer getGui() {
      return this;
   }

   public float getZLevel() {
      return this.zLevel;
   }

   public int getGuiLeft() {
      return this.guiLeft;
   }

   public int getGuiTop() {
      return this.guiTop;
   }

   public FontRenderer getFontRenderer() {
      return this.fontRendererObj;
   }

   public void _drawGradientRect(int left, int top, int right, int bottom, int colorStart, int colorEnd) {
      this.drawGradientRect(left, top, right, bottom, colorStart, colorEnd);
   }

   private class DecoButton extends GuiElement implements IGuiHandlesMouseClick, IGuiRenderable {
      public final ItemStack item;
      protected FontRenderer font;

      public DecoButton(ItemStack stack, int x, int y) {
         super(GuiDecobench.this, x, y, 16, 16);
         this.item = stack;
         this.tooltipEnabled = true;
      }

      public List getTooltipText(int mouseX, int mouseY) {
         this.tooltipText.clear();
         this.tooltipText.addAll(this.item.getTooltip(this.parent.getGui().mc.thePlayer, this.parent.getGui().mc.gameSettings.advancedItemTooltips));
         if (!((DecoModelMetadata)Props.modelData.get(this.item.getItemDamage())).isCraftable(GuiDecobench.this.table.getResources())) {
            this.tooltipText.add(EnumChatFormatting.LIGHT_PURPLE + StatCollector.translateToLocal("text.props.gui.resources.insufficient"));
         }

         return this.tooltipText;
      }

      public void render(int mouseX, int mouseY) {
         GL11.glPushMatrix();
         if (this.font == null) {
            this.font = this.item.getItem().getFontRenderer(this.item);
            if (this.font == null) {
               this.font = GuiDecobench.this.fontRendererObj;
            }
         }

         RenderHelper.enableGUIStandardItemLighting();
         GuiDecobench.itemRender.renderItemAndEffectIntoGUI(this.font, Minecraft.getMinecraft().getTextureManager(), this.item, this.posX(), this.posY());
         if (!((DecoModelMetadata)Props.modelData.get(this.item.getItemDamage())).isCraftable(GuiDecobench.this.table.getResources())) {
            GL11.glDisable(2896);
            GL11.glDisable(2929);
            GL11.glColorMask(true, true, true, false);
            this.parent._drawGradientRect(this.posX(), this.posY(), this.posX() + 16, this.posY() + 16, -1877995504, -1609560048);
            GL11.glColorMask(true, true, true, true);
            GL11.glEnable(2896);
            GL11.glEnable(2929);
         }

         RenderHelper.disableStandardItemLighting();
         GL11.glPopMatrix();
      }

      public void mouseClicked(int mouseButton) {
         if (mouseButton == 0) {
            GuiDecobench.this.table.onData(GuiDecobench.this.mc.thePlayer, this.item.getItemDamage(), GuiScreen.isShiftKeyDown());
         }

      }
   }
}
